﻿using AutoMapper;
using B.S.ProcessEgine.API.Applocations.Commands.FormTypeInfo;
using B.S.ProcessEgine.API.Dtos.ResponseDto;
using B.S.ProcessEgine.ErrorCode;
using B.S.ProcessEgine.Infractructure.Interfaces;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.ProcessEgine.API.Applocations.CommandHandlers.FormTypeInfo
{
    public class GetFormTypeCommandHandler : IRequestHandler<GetFormTypeCommand, ApiResult<IList<GetFormTypeReponseDto>>>
    {
        private readonly IFormTypeRepository formType;
        private readonly IMapper mapper;

        public GetFormTypeCommandHandler(IFormTypeRepository  formType, IMapper mapper)
        {
            this.formType = formType;
            this.mapper = mapper;
        }
        public async Task<ApiResult<IList<GetFormTypeReponseDto>>> Handle(GetFormTypeCommand request, CancellationToken cancellationToken)
        {
            //获取数据列表
            var formtypeList = await formType.GetAll().Where(x=>!x.IsDel).ToListAsync(cancellationToken);

            //转换类型
            var result = mapper.Map<IList<GetFormTypeReponseDto>>(formtypeList);

            return ApiResult<IList<GetFormTypeReponseDto>>.Success(result, StateCodeEnum.Ok);
        }
    }
}
